#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=..\path\dbsum.exe
#AutoIt3Wrapper_Compression=3
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.16.1
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here


#include <File.au3>
#include <MsgBoxConstants.au3>
#include <SQLite.au3>
#include <SQLite.dll.au3>
#include "findfile.au3"
;ConsoleWrite(@ScriptDir & @WorkingDir & @LocalAppDataDir & "\AutoIt v3\SQLite\" & @SystemDir & @WindowsDir & @CRLF)
; Generate a unique filename in @TempDir
;Local $sTempFile_1 = _TempFile()

; Generate a unique filename in the @HomeDrive directory and starting with the "prefix" labelled prefix_
;Local $sTempFile_2 = _TempFile(@HomeDrive & "\", "prefix_", ".txt", Default) ; Use the Default keyword to use the default parameters

;MsgBox($MB_SYSTEMMODAL, "", "Names suitable for temporary file usage: " & @CRLF & @CRLF & _
;               "File 1: " & $sTempFile_1 & @CRLF & _
;               "File 2: " & $sTempFile_2)
;#RequireAdmin

Main()

Func Main()
	_SQLite_Startup()
	If @error Then
			MsgBox($MB_SYSTEMMODAL, "SQLite Error", "SQLite3.dll Can't be Loaded!")
			Exit -1
	EndIf
	ConsoleWrite("_SQLite_LibVersion=" & _SQLite_LibVersion() & @CRLF)

	Local $sDbName = @WorkingDir & "\" & "index_hash" & ".db" ;_TempFile(@WorkingDir & "\", "index_", "db", 1)

	Local $sDrive = "", $sDir = "", $sFileName = "", $sExtension = ""
	Local $aPathSplit = _PathSplit(@ScriptFullPath, $sDrive, $sDir, $sFileName, $sExtension)
	;_ArrayDisplay($aPathSplit, "_PathSplit of " & @ScriptFullPath)
	; 设置db目录
	$sDbName = $aPathSplit[1] & "\index_hash.db"
	Local $hDskDb = _SQLite_Open($sDbName) ; Open a permanent disk database
	If @error Then
			MsgBox($MB_SYSTEMMODAL, "SQLite Error", "Can't open or create a permanent Database!")
			Exit -1
	EndIf
	; 创建数据表
	_SQLite_Exec(-1, "CREATE TABLE if not exists file_hash (a text, hash_str text, path text, check_time datetime);")
	;_SQLite_Exec(-1, "INSERT INTO test VALUES (" & _SQLite_FastEscape($sTestString) & ")")
	;_SQLite_Exec(-1, "INSERT INTO file_hash VALUES ('abc','hash_str','path', current_timestamp)")

	;$aLine = ReadExample()
	;if Not $aLine Then
	;	ConsoleWrite($aLine[0] & @CRLF)
	;	_SQLite_Exec(-1, "INSERT INTO file_hash VALUES ('abc', '"&$aLine[1]&"' , '"&$aLine[2]&"' , current_timestamp)")
	;endif

	;读取.sha1文件，写入数据表
	;$aFile = FindFile(@HomeDrive & "\" & @HomePath)
	$aFile = FindFile(@WorkingDir)
	;ConsoleWrite(_PathFull( "aaa.txt", @HomeDrive & "\" & @HomePath))

	Local $iRows = UBound($aFile, $UBOUND_ROWS)
	;_ArrayDisplay($aFile,'$aFile')
	ConsoleWrite($iRows & @CRLF)

	For $i = 1 To $iRows - 1 ; Loop through the array. UBound($aArray) can also be used.
		;MsgBox($MB_SYSTEMMODAL, "", $aArray[$i]) ; Display the contents of the array.
		;ConsoleWrite($i & @CRLF)
		;_ArrayDisplay($aFile,'$aFile')
		$fullpath = _PathFull($aFile[$i], @WorkingDir)
		ConsoleWrite($fullpath & @CRLF)
		ToDBExample( $fullpath)
	Next


	; we can use the 3 database as needed by refering to their handle

	; close the Dbs we created, in any order
	_SQLite_Close($hDskDb) ; DB is a regular file that could be reopened later

	; we don't really need that DB
	;FileDelete($sDbName)

	_SQLite_Shutdown()
EndFunc ;==>Main


;写入数据库表-----
Func ToDBExample($aFile)
	;$allLineArray = ReadAllExample(@HomeDrive & "\" & @HomePath & "\Downloads.sha1")
	$allLineArray = ReadAllExample($aFile)
	Local $iRows = UBound($allLineArray, $UBOUND_ROWS)
	;_ArrayDisplay($allLineArray,'$allLineArray')
	;ConsoleWrite($iRows & @CRLF)
	For $i = 0 To $iRows - 1 ; Loop through the array. UBound($aArray) can also be used.
		;MsgBox($MB_SYSTEMMODAL, "", $aArray[$i]) ; Display the contents of the array.
		;ConsoleWrite($i & @CRLF)
		;_ArrayDisplay($allLineArray,'$aLine')
		;ConsoleWrite($allLineArray[$i][0] & $allLineArray[$i][1] & @CRLF)
		_SQLite_Exec(-1, "INSERT INTO file_hash VALUES ('abc', '"&$allLineArray[$i][0]&"' , '"&$allLineArray[$i][1]&"' , current_timestamp)")
	Next

EndFunc   ;==>ToDBExample

;解析单个.sha文件---------------
Func ReadExample($sFilePath)
        ; Create a constant variable in Local scope of the filepath that will be read/written to.
        ;Local Const $sFilePath = @HomeDrive & "\" & @HomePath & "\Downloads.sha1"
		;ConsoleWrite($sFilePath & @CRLF)
		; Open the file for reading and store the handle to a variable.
        Local $hFileOpen = FileOpen($sFilePath, $FO_READ)
        If $hFileOpen = -1 Then
                MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
                Return False
        EndIf

        ; Read the fist line of the file using the handle returned by FileOpen.
        Local $sFileRead = FileReadLine($hFileOpen, 2)
        ; Close the handle returned by FileOpen.
        FileClose($hFileOpen)
		return SplitLine($sFileRead)
EndFunc   ;==>ReadExample

;读取文件所有行----------------
Func ReadAllExample($sFilePath)
		Local $aArrayRet
		Local $aArray = FileReadToArray($sFilePath);@ScriptFullPath)
        Local $iLineCount = @extended
        If @error Then
                MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file.
		Else
				Local $aKeys[0]
				Local $aItems[0]
				;Local $oDi = ObjCreate('scripting.dictionary')
				;For $i = 0 To 10
				;		$oDi.Add('key' & $i, 'item' & $i)
				;Next

				;_ArrayDisplay(_Array2DCreate($oDi.Keys, $oDi.Items))

                For $i = 0 To $iLineCount - 1 ; Loop through the array. UBound($aArray) can also be used.
					;MsgBox($MB_SYSTEMMODAL, "", $aArray[$i]) ; Display the contents of the array.
					Local $lineArray
					$lineArray = SplitLine($aArray[$i])
					;_ArrayDisplay($lineArray, '$lineArray[' & $i)
					ConsoleWrite("No.: " & $i & @CRLF)
					If $lineArray = null Then
						;_ArrayDisplay($lineArray ,'$lineArray')
						;ConsoleWrite($lineArray[1] & @CRLF)
					else
						;_ArrayDisplay($lineArray, 'Err: $lineArray')
						;ConsoleWrite($lineArray[1] & @CRLF)
						;$oDi.Add($lineArray[1], $lineArray[2])
						_ArrayAdd($aKeys, $lineArray[1])
						_ArrayAdd($aItems, $lineArray[2])
					endif
                Next

				;$aArrayRet =_Array2DCreate($oDi.Keys, $oDi.Items)
				$aArrayRet =_Array2DCreate($aKeys, $aItems)
				;_ArrayDisplay($aArrayRet,'$aArrayRet')
		EndIf
		return $aArrayRet
EndFunc   ;==>ReadAllExample

;分割一行----------------
Func SplitLine($aLineStr)
		Local $ret = null
		If StringInStr($aLineStr, "; ", 0, 1, 1) = 0 Then ; 非注释行
			Local $aLine = StringSplit($aLineStr, " ")
			; Display the first line of the file.
			;ConsoleWrite($aLine[1] & @CRLF)
			$ret = $aLine
		endif
		return $ret
EndFunc   ;==>SplitLine